package com.dhh.sky.dao;

import java.util.*;
import com.dhh.sky.model.Operation;
import com.dhh.sky.util.Constant;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class OperationDAO {
	private DataBaseHelper db;
	private final String TABLE = "operation";
	
	public OperationDAO(Context ctx){
		db = new DataBaseHelper(ctx);
	}
	
	public List<Operation> getOperationAll(int action){
		List<Operation> rlt = new ArrayList<Operation>();
		Cursor cursor = null;
		try{
			db.openDataBase();
			String where = "action="+action;
			cursor = db.getReadableDatabase().query(true, TABLE, Operation.getColumns(), where,null, null, null, null, null);
			if(cursor!=null && cursor.moveToFirst()){
				do{
					rlt.add(initOperation(cursor));
				}while(cursor.moveToNext());
			}
		}catch(Exception e){
			Log.e(Constant.TAG, "Get operation error.", e);
		}finally{
			if(cursor!=null) cursor.close();
			if(db!=null) db.close();
		}
		return rlt;
	}
	
	public Operation getOperation(String path, int action){
		Operation op = null;
		Cursor cursor = null;
		try{
			db.openDataBase();
			String where = "path='"+path+"' and action="+action;
			cursor = db.getReadableDatabase().query(true, TABLE, Operation.getColumns(), where,null, null, null, null, null);
			if(cursor!=null && cursor.moveToFirst()){
				op = initOperation(cursor);
			}
		}catch(Exception e){
			Log.e(Constant.TAG, "Get operation error.", e);
		}finally{
			if(cursor!=null) cursor.close();
			if(db!=null) db.close();
		}
		return op;
	}
	
	public Operation getOperation(String path, int action,int status){
		Operation op = null;
		Cursor cursor = null;
		try{
			db.openDataBase();
			String where = "path='"+path+"' and action="+action+"' and status="+status;;
			cursor = db.getReadableDatabase().query(true, TABLE, Operation.getColumns(), where,null, null, null, null, null);
			if(cursor!=null && cursor.moveToFirst()){
				op = initOperation(cursor);
			}
		}catch(Exception e){
			Log.e(Constant.TAG, "Get operation error.", e);
		}finally{
			if(cursor!=null) cursor.close();
			if(db!=null) db.close();
		}
		return op;
	}
	
	public List<Operation> getOperation(int action, int status){
		List<Operation> rlt = new ArrayList<Operation>();
		Cursor cursor = null;
		try{
			db.openDataBase();
			String where = "action="+action+" and status="+status;
			cursor = db.getReadableDatabase().query(true, TABLE, Operation.getColumns(), where,null, null, null, null, null);
			if(cursor!=null && cursor.moveToFirst()){
				do{
					rlt.add(initOperation(cursor));
				}while(cursor.moveToNext());
			}
		}catch(Exception e){
			Log.e(Constant.TAG, "Get operation error.", e);
		}finally{
			if(cursor!=null) cursor.close();
			if(db!=null) db.close();
		}
		return rlt;
	}
	
	public List<Operation> getOperation(int status){
		List<Operation> rlt = new ArrayList<Operation>();
		Cursor cursor = null;
		try{
			db.openDataBase();
			String where = "status="+status;
			cursor = db.getReadableDatabase().query(true, TABLE, Operation.getColumns(), where,null, null, null, null, null);
			if(cursor!=null && cursor.moveToFirst()){
				do{
					rlt.add(initOperation(cursor));
				}while(cursor.moveToNext());
			}
		}catch(Exception e){
			Log.e(Constant.TAG, "Get operation error.", e);
		}finally{
			if(cursor!=null) cursor.close();
			if(db!=null) db.close();
		}
		return rlt;
	}
	
	public boolean removeOperation(String path, int action){
		boolean rlt = false;
		
		try{
			db.openDataBase();
			String where = "path='"+path+"' and action="+action;
			db.getWritableDatabase().delete(TABLE, where,null);
			rlt = true;
		}catch(Exception e){
			Log.e(Constant.TAG, "Remove operation error.", e);
		}finally{
			if(db!=null) db.close();
		}
		
		return rlt;
	}
	
	public boolean removeOperation(int action){
		boolean rlt = false;
		
		try{
			db.openDataBase();
			String where = "action="+action;
			db.getWritableDatabase().delete(TABLE, where,null);
			rlt = true;
		}catch(Exception e){
			Log.e(Constant.TAG, "Remove operation error.", e);
		}finally{
			if(db!=null) db.close();
		}
		
		return rlt;
	}
	
	public boolean updateStatus(String path, int action,int status){
		boolean rlt = false;
		
		try{
			db.openDataBase();
			String where = "path='"+path+"' and action="+action;
			ContentValues values = new ContentValues();
			values.put("status", status);
			db.getWritableDatabase().update(TABLE, values, where, null);
			rlt = true;
		}catch(Exception e){
			Log.e(Constant.TAG, "Remove operation error.", e);
		}finally{
			if(db!=null) db.close();
		}
		
		return rlt;
	}
	
	public boolean update(Operation op){
		boolean rlt = false;
		
		try{
			db.openDataBase();
			String where = "path='"+op.getPath()+"' and action="+op.getAction();
			ContentValues values = new ContentValues();
			values.put("target", op.getTarget());
			values.put("status", op.getStatus());
			values.put("message", op.getMessage());
			values.put("meta", op.getMeta());
			db.getWritableDatabase().update(TABLE, values, where, null);
			rlt = true;
		}catch(Exception e){
			Log.e(Constant.TAG, "update() error.", e);
		}finally{
			if(db!=null) db.close();
		}
		
		return rlt;
	}
	
	public boolean addOperation(String path,String target,int action,int status,String meta){
		boolean flag = true;
		try{
			db.openDataBase();
			ContentValues values = new ContentValues();
			values.put("path", path);
			values.put("target", target);
			values.put("action", action);
			values.put("status", status);
			values.put("meta", meta);
			long rlt = db.getWritableDatabase().insert(TABLE, null, values);		
			flag = rlt > -1;
		}catch(Exception e){
			Log.e(Constant.TAG, "Create operation error.", e);
		}finally{
			if(db!=null) db.close();
		}
		return flag;
	}
	
	private Operation initOperation(Cursor cursor){
		Operation op = new Operation();
		op.setPath(cursor.getString(0));
		op.setTarget(cursor.getString(1));
		op.setAction(cursor.getString(2));
		op.setStatus(cursor.getString(3));
		op.setMessage(cursor.getString(4));
		op.setMeta(cursor.getString(5));
		return op;
	}
}

